// 导入http模块
const http = require("http");

// 创建服务对象
const server = http.createServer((request, response) => {
  // 实例化一个 url 对象
  let url = new URL(request.url, "http://127.0.0.1");
  console.log(url);
  /* URL {
  href: 'http://127.0.0.1/search?keyword=h5&num=1',
  origin: 'http://127.0.0.1',
  protocol: 'http:',
  host: '127.0.0.1',
  hostname: '127.0.0.1',
  port: '',
  pathname: '/search',
  search: '?keyword=h5&num=1',
  searchParams: URLSearchParams { 'keyword' => 'h5', 'num' => '1' },
  hash: ''
} */
  // 获取url的路径
  console.log(url.pathname); // /search
  // 获取url的查询字符串
  console.log(url.searchParams.get('keyword')); // h5

  response.end("url");
});

// 3.监听端口，启动服务
server.listen(9000, () => {
  console.log("服务在9000端口启动");
});
